package MapReduce.Demo8_TemperatureCountByMonth;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import utils.JobSubmit;

import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/21 8:47
 */
public class TemperatureCountByMonthApp {

    public static class TCBMMApper extends Mapper<Text, Text,Text, IntWritable>{

        IntWritable temperature = new IntWritable();

        @Override
        protected void map(Text key, Text value, Mapper<Text, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            temperature.set(Integer.parseInt(value.toString()));

            context.write(key,temperature);
        }
    }

    public static class TCBMReducer extends Reducer<Text,IntWritable,Text,IntWritable>{

        IntWritable temperatureCount = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            int tempSum = 0;
            for (IntWritable value : values) {
                tempSum += value.get();
            }

            temperatureCount.set(tempSum);

            context.write(key,temperatureCount);

        }
    }

    public static void main(String[] args) {

        JobSubmit.submitBaseJob(
                TemperatureCountByMonthApp.class,
                args,
                TemperatureCountByMonthCombiner.class,
                KeyValueTextInputFormat.class

        );

    }

}
